home *** CD-ROM | disk | FTP | other *** search
- Path: pwolf-mac.qualcomm.com!user
- From: pwolf@qualcomm.com (Paul I. Wolf )
- Newsgroups: comp.lang.c
- Subject: Re: Is this somekind of a stack fault or what?
- Date: 11 Jan 1996 19:24:47 GMT
- Organization: Qualcomm, Inc.
- Message-ID: <pwolf-1101961124440001@pwolf-mac.qualcomm.com>
- References: <4d2oi6$ghf@gate.compart.fi>
- NNTP-Posting-Host: pwolf-mac.qualcomm.com
-
- In article <4d2oi6$ghf@gate.compart.fi>, joonas.kervinen@pcb.compart.fi
- (joonas kervinen) wrote:
-
- > I have a function that causes a bizarre problem. It's used in a
- > Windows program and a DOS version compiled with BCC 4.53 large model.
- > Everything is just straight C. I'm pretty sure this function is the
- > culprit since slight modifications to it (as described in the source)
- > make everything go all right.
- >
- > The function purpose is to round() a number to certain decimal
- > accuracy. It is passed the number as a char pointer (char *sa2) and
- > the desired decimal count (deci). While the function is not foolproof
- > (it won't handle negative decimals or other clear errors) it is
- > sufficient for me. I've found out that sprintf() are not reliable in
- > rounding up situation (do a sprintf(mystr,"%f.2d",1.499) or something
- > like that and you'll get errors)
- >
- > I can't figure why the func bombs stack or whatever but it seems to
- > overwrite memory or unbalance the stack. When I do a
- > roundx(" 9.99 ",1)
- > I do get a pointer to "10.0". The problem is that after that strange
- > things happen to the rest of my program. Not a big crash but some
- > memory or pointer go away. I've carefully examined the func under
- > Turbo debugger and it just doesn't overwrite memory. I tend to think
- > that there is something wrong with this:
- >
- > char *roundx(char *sa2, int deci)
- > {
- > /* */
- > static char sa1=" ";
- > /* rest of program */
- > return sa1;
- > }
- >
- > When I change it to:
- > char *roundx(char *sa2, int deci)
- > {
- > /* */
- > char sa1[20];
- > /* rest of program with one modification*/
- > return sa1;
- > }
- > ... everything works on DOS and Windows version.
- >
- > Please tell me what's going on. Please answer via E-mail since I don't
- > come to comp.lang.c often (this is also on Windows programming
- > section)
-
- I didn't inspect your actual function logic, bu try changing your
- declaration to either one of:
-
- static char* sa1 = " ";
- static char sa1 [] = " ";
-
- Your compiler should have produced some warning about the return value.
-